package request

import (
	"YingShop/log"
	"crypto/tls"
	"fmt"
	"io"
	"net/http"
	netUrl "net/url"
)

func Get(url string, headers map[string]string, params map[string]string) (content []byte, err error) {
	queryParams := netUrl.Values{}
	tr := &http.Transport{

		TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 这里只是示例，不推荐在实际环境中跳过证书验证

	}

	// 创建一个http.Client，使用自定义的Transport

	client := &http.Client{Transport: tr}

	for key, value := range params {
		queryParams.Add(key, value)
	}
	queryBody := queryParams.Encode()
	//发起请求
	completeUrl := url + "?" + queryBody
	log.Info(fmt.Sprintf("query url = %s", completeUrl))
	res, err := client.Get(completeUrl)
	if err != nil {
		fmt.Println("get请求失败：", err)
	}
	//延迟关闭（返回response的body尽量关闭,避免内存泄露）
	defer res.Body.Close()

	//读取body数据
	content, err = io.ReadAll(res.Body)
	if err != nil {
		fmt.Println("读取body失败：", err)
		return
	}
	return
}
